package com.julang.check.controller;

import com.julang.check.bean.CommonEntity;
import com.julang.check.bean.User;
import com.julang.check.bean.pay.PayData;
import com.julang.check.api.mapper.UserMapper;
import com.julang.check.utils.CommonData;
import com.julang.check.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.julang.check.utils.MessageInfo.ORDERNOTEXIST;
import static com.julang.check.utils.MessageInfo.ORDERNOTEXIST;
/**
 * @author 赵自强
 * */
@RestController
@RequestMapping(value = "/api/trade")
public class TradeController extends BaseController{
    @Resource
    private UserMapper userMapper;
    @GetMapping(value = "/all_trades")
    @ResponseBody
    public Result<List<CommonEntity.OrderDetail>> getAllTradeById() {
        String userId = this.getUserId();
        return Result.buildSuccess(PayData.getOrderByUserId(userId));
    }
    @GetMapping(value = "/cancel_trades")
    @ResponseBody
    public Result<String> cancelTrade(@RequestParam("out_trade_no") String outTradeNo) {
        boolean isCancel =  PayData.makeOrderFinished(outTradeNo, 2, "-", "-");
        if (isCancel){
            return Result.buildSuccess();
        }else {
            return Result.buildError(ORDERNOTEXIST, "");
        }
    }
    @GetMapping(value ="/order_state")
    @ResponseBody
    public Result<User> getOrderState() {
        String userId = this.getUserId();
        Result<User> result = new Result<>();
        if (PayData.haveUnusedOrderByUserId(userId)) {
            result.fail(CommonData.FAIL,"", null);
        }else {
            result.success("", userMapper.selectById(userId));
        }
        return result;
    }
}
